package com.ali.gulimall.product.service.impl;

import com.ali.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.ali.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.ali.gulimall.product.entity.AttrEntity;
import com.ali.gulimall.product.service.AttrService;
import com.ali.gulimall.product.vo.AttrGroupRelationVo;
import com.ali.gulimall.product.vo.AttrGroupVO;
import com.ali.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.bouncycastle.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ali.common.utils.PageUtils;
import com.ali.common.utils.Query;

import com.ali.gulimall.product.dao.AttrGroupDao;
import com.ali.gulimall.product.entity.AttrGroupEntity;
import com.ali.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }


    //根据id查取分页数据
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String)params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        //0是代表查询所有
        if(catelogId == 0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);

        }else{
            wrapper.eq("catelog_Id",catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
            return new PageUtils(page);

        }
    }


    //只删除关联关系，属性不删除
    @Override
    public void deletRelation(AttrGroupRelationVo[] vos) {
//        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
//        for(int i = 0;i<vos.length;i++){
//            wrapper.eq("attr_id",vos[i].getAttrId()).eq("attr_group_id",vos[i].getAttrGroupId());
//        }
        List<AttrAttrgroupRelationEntity> collect = Arrays.asList(vos).stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationDao.deleteBatchRelation(collect);
    }

    //根据分类id查出所有的分组和这些组的属性
    @Override
    public List<AttrGroupVO> getAttrGroupWithAttersByCateloId(Long catelogId) {

        //查询分组id
        List<AttrGroupEntity> attrGroupEntities = this.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId)
        );
        List<AttrGroupVO> collect = attrGroupEntities.stream().map((item) -> {
            AttrGroupVO attrGroupVO = new AttrGroupVO();
            BeanUtils.copyProperties(item, attrGroupVO);

            List<AttrEntity> attr = attrService.getRelationAttr(item.getAttrGroupId());
            attrGroupVO.setAttrs(attr);

            return attrGroupVO;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        //1、查出当前spu对应的所有属性的分组信息以及当前分组下的所有属性对应的值
        AttrGroupDao baseMapper = this.getBaseMapper();
        List<SpuItemAttrGroupVo> vos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return vos;
    }

}